package com.peko.interceptor.interceptor;

import com.peko.interceptor.controller.MyController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Peko
 */
public class MyInterceptor1 implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(MyController.class);

    /**
     *
     * @param request
     * @param response
     * @param handler 当前请求的控制器的方法对象
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("=========1========");

        //true 放行 false 中断
        return true;
    }

    /**
     *
     * @param request
     * @param response
     * @param handler  当前请求的控制器的方法对象
     * @param modelAndView   模型和视图  当前请求访问方法的modelandview
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        logger.info("=========2========");
    }

    /**
     * 这个方法相当于 finally{}代码块，即无论请求是否有问题都会执行
     * @param request
     * @param response
     * @param handler  当前请求的控制器的方法对象
     * @param ex  如果控制器出现异常时异常对象
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        logger.info("=========3========");
    }
}
